package com.sixth.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.util.StringUtil;
import com.sixth.bean.MarketAd;
import com.sixth.bean.MarketAdExample;
import com.sixth.bean.bo.AdvBeanBO;
import com.sixth.bean.bo.AdvPageInfo;
import com.sixth.bean.vo.BaseListVO;
import com.sixth.bean.vo.WXHomeIndexBannerVO;
import com.sixth.constant.Constant;
import com.sixth.constant.PropertiesConstant;
import com.sixth.mapper.MarketAdMapper;
import com.sixth.service.AdvService;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.Date;

import java.util.ArrayList;

import java.util.List;

@Service
public class AdvServiceImpl implements AdvService {



    //广告查询类mapper
    @Autowired
    MarketAdMapper adMapper;

    /**
     * @Author Chen
     * @Date 2022-09-06  23-33-13
     * @param
     * @return {@link null }
     * @description 根据条件查询广告表数据
     */
    @Override
    public BaseListVO<MarketAd> getAdvList(AdvPageInfo pageInfo) {

        PageHelper.startPage(pageInfo.getPage(), pageInfo.getLimit());

        MarketAdExample adExample = new MarketAdExample();

        adExample.setDistinct(true);

        adExample.setOrderByClause(pageInfo.getSort()+" "+pageInfo.getOrder());

        MarketAdExample.Criteria criteria = adExample.createCriteria();

        criteria.andDeletedEqualTo(false);

        if (!StringUtil.isEmpty(pageInfo.getName())){
            criteria.andNameLike("%"+pageInfo.getName()+"%");
        }

        if (!StringUtil.isEmpty(pageInfo.getContent())){
            criteria.andContentLike("%"+pageInfo.getContent()+"%");
        }

        List<MarketAd> marketAds = adMapper.selectByExample(adExample);
        PageInfo<MarketAd> page = new PageInfo<MarketAd>(marketAds);

        BaseListVO baseListVO = new BaseListVO();

        baseListVO.setPage(page.getPageNum());
        baseListVO.setLimit(page.getPageSize());
        baseListVO.setPages(page.getPages());
        baseListVO.setTotal(page.getTotal());

        baseListVO.setList(marketAds);

        return baseListVO;
    }

    @Override
    /**
     * @Author Chen
     * @Date 2022-09-07  14-40-33
     * @param advBeanBO
     * @return {@link int }
     * @description 逻辑删除广告表中一条数据
     */
    public int deleteAdv(AdvBeanBO advBeanBO) {

        MarketAd marketAd = new MarketAd();

        marketAd.setId(advBeanBO.getId());
        marketAd.setName(advBeanBO.getName());
        marketAd.setContent(advBeanBO.getContent());
        marketAd.setAddTime(advBeanBO.getAddTime());
        marketAd.setEnabled(advBeanBO.getEnabled());
        marketAd.setLink(advBeanBO.getLink());
        marketAd.setPosition(advBeanBO.getPosition());
        marketAd.setUpdateTime(advBeanBO.getUpdateTime());
        marketAd.setUrl(advBeanBO.getUrl());
        marketAd.setStartTime(advBeanBO.getStartTime());
        marketAd.setEndTime(advBeanBO.getEndTime());
        marketAd.setDeleted(true);


        int delResult = adMapper.updateByPrimaryKey(marketAd);

        if (delResult == Constant.ONE){
            return Constant.OK;
        }

        return Constant.NON;
    }


    /**
     *
     * @author qys
     * @date 2022/9/9 20:04
     * @param marketAd
     * @return MarketAd
     */
    @Override
    public MarketAd adminAdCreate(MarketAd marketAd) {
        marketAd.setAddTime(new Date());
        marketAd.setUpdateTime(new Date());
        if (marketAd.getEnabled()) {
            // 新增为启用，添加启用时间
            marketAd.setStartTime(new Date());
        }
        // String url = marketAd.getUrl();
        // 只保留uri
        // String uri = new StringBuffer().append("http://").append(Constant.SERVER_IP).append(":").append(Constant.SERVER_PORT).toString();
        // marketAd.setUrl(url.replace());
        adMapper.insertSelective(marketAd);
        return marketAd;
    }

    /**
     *
     * @author qys
     * @date 2022/9/9 20:04
     * @param marketAd
     * @return MarketAd
     */
    @Override
    public MarketAd adminAdUpdate(MarketAd marketAd) {
        MarketAd temp = adMapper.selectByPrimaryKey(marketAd.getId());
        // 看是否改变了启用状态
        if (temp.getEnabled() != marketAd.getEnabled()) {
            // 改变启用状态，且改为启用，应修改启动时间
            if (marketAd.getEnabled()) {
                marketAd.setStartTime(new Date());
            } else {
                // 改为不启用，应修改结束时间
                marketAd.setEndTime(new Date());
            }
        }
        marketAd.setUpdateTime(new Date());
        adMapper.updateByPrimaryKeySelective(marketAd);
        return marketAd;
    }


    /**
     * lixinhang
     * @return
     * 新增进入小程序首页，banner的回显,按照id升序排列
     */
    @Override
    public List<WXHomeIndexBannerVO> indexBanner() {
        MarketAdExample example = new MarketAdExample();
        example.setDistinct(true);
        example.setOrderByClause("id asc");
        MarketAdExample.Criteria criteria = example.createCriteria();
        criteria.andDeletedEqualTo(false);

        List<MarketAd> marketAds = adMapper.selectByExample(example);
        List<WXHomeIndexBannerVO> banner = new ArrayList<>();
        for (MarketAd marketAd : marketAds) {
            WXHomeIndexBannerVO wxHomeIndexBannerVO = new WXHomeIndexBannerVO();
            try {
                BeanUtils.copyProperties(wxHomeIndexBannerVO,marketAd);
            } catch (Exception e) {
                e.printStackTrace();
            }
            banner.add(wxHomeIndexBannerVO);
        }

        return banner;
    }

}
